/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author alfredo
 */
public class RMSUtil {

    private static RecordStore recordStore = null;
    public static String registroCuenta = "cuenta";
    public static String registroMovimiento = "movimiento";

    public static void open(String fileName) {
        try {
            recordStore = RecordStore.openRecordStore(fileName, true);
        } catch (RecordStoreException rse) {
        }
    }

    public static void close() throws RecordStoreNotOpenException,
            RecordStoreException {
        if (recordStore.getNumRecords() == 0) {
            //String fileName = recordStore.getName();
            recordStore.closeRecordStore();
            //recordStore.deleteRecordStore(fileName);
        } else {
            recordStore.closeRecordStore();
        }
    }

    public static RecordStore getRecordStore() {
        return recordStore;
    }

    public static void setRecordStore(RecordStore recordStore) {
        RMSUtil.recordStore = recordStore;
    }

    public static void addRecord(String recordStore, String registro) {
        String record = registro;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            outputStream.writeUTF(record);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
        byte[] b = baos.toByteArray();
        try {
            open(recordStore);
            getRecordStore().addRecord(b, 0, b.length);
            close();
        } catch (RecordStoreException rse) {
            System.out.println(rse);
        }
    }

    public static void updateRecord(String recordStore, int id, String registro) {
        String record = registro;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream outputStream = new DataOutputStream(baos);
        try {
            outputStream.writeUTF(record);
        } catch (IOException ioe) {
            System.out.println(ioe);
        }
        byte[] b = baos.toByteArray();
        try {
            open(recordStore);
            getRecordStore().setRecord(id, b, 0, b.length);
            close();
        } catch (RecordStoreException rse) {
            System.out.println(rse);
        } finally {
        }
    }

    public static String getRecord(String recordStore, int id) {
        String ret = null;
        open(recordStore);
        try {
            ret = byteArrayToString(getRecordStore().getRecord(id)) + "" + (id) + ";";
            close();
        } catch (RecordStoreNotOpenException ex) {
        } catch (RecordStoreException ex) {
        }
        return ret;
    }

    public static int count(String recordStore) {
        int ret = 0;
        try {
            open(recordStore);
            ret = getRecordStore().getNumRecords();
            close();
        } catch (RecordStoreNotOpenException ex) {
        } catch (RecordStoreException ex) {
        }
        return ret;
    }

    public static String[] list(String recordStore) {
        String[] ret = null;
        try {
            open(recordStore);
            int totalRegistros = getRecordStore().getNumRecords();
            ret = new String[totalRegistros];
            for (int i = 0; i < totalRegistros; i++) {
                ret[i] = byteArrayToString(getRecordStore().getRecord(i + 1)) + "" + (i + 1) + ";";
            }
            close();
        } catch (RecordStoreNotOpenException ex) {
        } catch (RecordStoreException ex) {
        }

        return ret;
    }

    public static String[] list(String recordStore, int npagina, int cantidad) {
        String[] ret = null;
        int totalRegistros, rfinal, rinicio;
        try {
            open(recordStore);
            totalRegistros = getRecordStore().getNumRecords();
            if (totalRegistros > 0) {
                rinicio = totalRegistros - (npagina * cantidad);
                if (rinicio < 1) {
                    rinicio = 1;
                }
                rfinal = rinicio - cantidad+1;
                if (rfinal < 1) {
                    rfinal = 1;
                }
                ret = new String[rinicio-rfinal+1];
                int j = 0;
                for (int i = rinicio; i >= rfinal; i--) {
                    ret[j++] = byteArrayToString(getRecordStore().getRecord(i)) + "" + (i) + ";";
                }


            }
            close();
        } catch (RecordStoreNotOpenException ex) {
        } catch (RecordStoreException ex) {
        }
        return ret;
    }

    public static String byteArrayToString(byte[] data) {
        String ret = "";
        try {
            ByteArrayInputStream bin = new ByteArrayInputStream(data);
            DataInputStream din = new DataInputStream(bin);
            ret = din.readUTF();
        } catch (IOException ex) {
        }
        return ret;
    }
}
